package utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 雷哥
 * @Date: 2020/10/06/17:22
 * @Description:  工具类
 */
public class RabbitMQUtils {

  private static ConnectionFactory connectionFactory;
  static {
    connectionFactory=new ConnectionFactory();
    //2,设置相关的参数
    connectionFactory.setHost("116.62.44.5");
    connectionFactory.setPort(5672);
    connectionFactory.setUsername("sxt");
    connectionFactory.setPassword("123456");
    connectionFactory.setVirtualHost("/v-sxt");
  }


  /**
   * 得到连接的方法
   */
  public static Connection getConnection(){
    try {
      //从连接工厂里面创建一个连接
      Connection connection = connectionFactory.newConnection();
      return  connection;
    }catch (Exception e){
      System.out.println(e);
    }
    return  null;
  }

  /**
   * 关闭连接的方法
   */
  public static  void closeChannelAndConnection(Channel channel,Connection connection){
    try {
      if(null!=channel) channel.close();
      if(null!=connection) connection.close();
    }catch (Exception e){
      System.out.println(e);
    }
  }

}
